home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2000 September / september_2000.iso / intercd / root / ^Linux / WindowMaker / WINGs / wlabel.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-09  |  4.3 KB  |  243 lines

  1.  
  2.  
  3.  
  4.  
  5. #include "WINGsP.h"
  6.  
  7.  
  8.  
  9. typedef struct W_Label {
  10.     W_Class widgetClass;
  11.     W_View *view;
  12.     
  13.     char *caption;
  14.  
  15.     WMColor *textColor;
  16.     WMFont *font;               /* if NULL, use default */
  17.     
  18.     W_Pixmap *image;
  19.  
  20.     struct {
  21.     WMReliefType relief:3;
  22.     WMImagePosition imagePosition:4;
  23.     WMAlignment alignment:2;
  24.     
  25.     unsigned int noWrap:1;
  26.  
  27.     unsigned int redrawPending:1;
  28.     } flags;
  29. } Label;
  30.  
  31.  
  32. #define DEFAULT_WIDTH        60
  33. #define DEFAULT_HEIGHT        14
  34. #define DEFAULT_ALIGNMENT    WALeft
  35. #define DEFAULT_RELIEF        WRFlat
  36. #define DEFAULT_IMAGE_POSITION    WIPNoImage
  37.  
  38.  
  39. static void destroyLabel(Label *lPtr);
  40. static void paintLabel(Label *lPtr);
  41.  
  42.  
  43. static void handleEvents(XEvent *event, void *data);
  44.  
  45.  
  46. WMLabel*
  47. WMCreateLabel(WMWidget *parent)
  48. {
  49.     Label *lPtr;
  50.     
  51.     lPtr = wmalloc(sizeof(Label));
  52.     memset(lPtr, 0, sizeof(Label));
  53.  
  54.     lPtr->widgetClass = WC_Label;
  55.     
  56.     lPtr->view = W_CreateView(W_VIEW(parent));
  57.     if (!lPtr->view) {
  58.     wfree(lPtr);
  59.     return NULL;
  60.     }
  61.     lPtr->view->self = lPtr;
  62.     
  63.     lPtr->textColor = WMRetainColor(lPtr->view->screen->black);
  64.     
  65.     WMCreateEventHandler(lPtr->view, ExposureMask|StructureNotifyMask,
  66.              handleEvents, lPtr);
  67.  
  68.     W_ResizeView(lPtr->view, DEFAULT_WIDTH, DEFAULT_HEIGHT);
  69.     lPtr->flags.alignment = DEFAULT_ALIGNMENT;
  70.     lPtr->flags.relief = DEFAULT_RELIEF;
  71.     lPtr->flags.imagePosition = DEFAULT_IMAGE_POSITION;
  72.  
  73.     return lPtr;
  74. }
  75.  
  76.  
  77. void
  78. WMSetLabelImage(WMLabel *lPtr, WMPixmap *image)
  79. {
  80.     if (lPtr->image!=NULL) 
  81.     WMReleasePixmap(lPtr->image);
  82.     
  83.     if (image)
  84.     lPtr->image = WMRetainPixmap(image);
  85.     else
  86.     lPtr->image = NULL;
  87.  
  88.     if (lPtr->view->flags.realized) {
  89.     paintLabel(lPtr);
  90.     }
  91. }
  92.  
  93.  
  94. WMPixmap*
  95. WMGetLabelImage(WMLabel *lPtr)
  96. {
  97.     return lPtr->image;
  98. }
  99.  
  100.  
  101. void
  102. WMSetLabelImagePosition(WMLabel *lPtr, WMImagePosition position)
  103. {
  104.     lPtr->flags.imagePosition = position;
  105.     if (lPtr->view->flags.realized) {
  106.     paintLabel(lPtr);
  107.     }
  108. }
  109.  
  110.     
  111. void
  112. WMSetLabelTextAlignment(WMLabel *lPtr, WMAlignment alignment)
  113. {
  114.     lPtr->flags.alignment = alignment;
  115.     if (lPtr->view->flags.realized) {
  116.     paintLabel(lPtr);
  117.     }
  118. }
  119.  
  120.  
  121. void
  122. WMSetLabelRelief(WMLabel *lPtr, WMReliefType relief)
  123. {
  124.     lPtr->flags.relief = relief;
  125.     if (lPtr->view->flags.realized) {
  126.     paintLabel(lPtr);
  127.     }
  128. }
  129.  
  130.  
  131. void
  132. WMSetLabelText(WMLabel *lPtr, char *text)
  133. {
  134.     if (lPtr->caption)
  135.     wfree(lPtr->caption);
  136.     
  137.     if (text!=NULL) {
  138.     lPtr->caption = wstrdup(text);
  139.     } else {
  140.     lPtr->caption = NULL;
  141.     }
  142.     if (lPtr->view->flags.realized) {
  143.     paintLabel(lPtr);
  144.     }
  145. }
  146.  
  147.  
  148. void
  149. WMSetLabelFont(WMLabel *lPtr, WMFont *font)
  150. {    
  151.     if (lPtr->font!=NULL)
  152.     WMReleaseFont(lPtr->font);
  153.     if (font)
  154.     lPtr->font = WMRetainFont(font);
  155.     else
  156.     lPtr->font = NULL;
  157.  
  158.     if (lPtr->view->flags.realized) {
  159.     paintLabel(lPtr);
  160.     }
  161. }
  162.  
  163.  
  164. void
  165. WMSetLabelTextColor(WMLabel *lPtr, WMColor *color)
  166. {
  167.     if (lPtr->textColor)
  168.     WMReleaseColor(lPtr->textColor);
  169.     lPtr->textColor = WMRetainColor(color);
  170. }
  171.  
  172.  
  173. void
  174. WMSetLabelWraps(WMLabel *lPtr, Bool flag)
  175. {
  176.     if (lPtr->flags.noWrap != !flag) {
  177.     lPtr->flags.noWrap = !flag;
  178.     if (lPtr->view->flags.realized)
  179.         paintLabel(lPtr);
  180.     }
  181. }
  182.  
  183.  
  184. static void
  185. paintLabel(Label *lPtr)
  186. {
  187.     W_Screen *scrPtr = lPtr->view->screen;
  188.     GC gc;
  189.     
  190.     if (lPtr->textColor)
  191.     gc = WMColorGC(lPtr->textColor);
  192.     else
  193.     gc = WMColorGC(scrPtr->black);
  194.  
  195.     W_PaintTextAndImage(lPtr->view, !lPtr->flags.noWrap, gc,
  196.             (lPtr->font!=NULL ? lPtr->font : scrPtr->normalFont),
  197.             lPtr->flags.relief, lPtr->caption,
  198.             lPtr->flags.alignment, lPtr->image, 
  199.             lPtr->flags.imagePosition, NULL, 0);
  200. }
  201.  
  202.  
  203.  
  204. static void
  205. handleEvents(XEvent *event, void *data)
  206. {
  207.     Label *lPtr = (Label*)data;
  208.  
  209.     CHECK_CLASS(data, WC_Label);
  210.  
  211.  
  212.     switch (event->type) {
  213.      case Expose:
  214.     if (event->xexpose.count!=0)
  215.         break;
  216.     paintLabel(lPtr);
  217.     break;
  218.     
  219.      case DestroyNotify:
  220.     destroyLabel(lPtr);
  221.     break;
  222.     }
  223. }
  224.  
  225.  
  226. static void
  227. destroyLabel(Label *lPtr)
  228. {
  229.     if (lPtr->textColor)
  230.     WMReleaseColor(lPtr->textColor);
  231.     
  232.     if (lPtr->caption)
  233.     wfree(lPtr->caption);
  234.  
  235.     if (lPtr->font)
  236.     WMReleaseFont(lPtr->font);
  237.  
  238.     if (lPtr->image)
  239.     WMReleasePixmap(lPtr->image);
  240.  
  241.     wfree(lPtr);
  242. }
  243.